import React, { Component } from 'react';
import { Affix, Button, Form } from 'antd';
import { messages } from 'share/common';
import { formList } from 'containers/request-and-expense-settings/expense-setting/data-sources';
import requestAndExpenseService from 'containers/request-and-expense-settings/request-and-expense-settings.service';
import 'styles/request-and-expense-settings/print-free.scss';

@Form.create()
class PrintFree extends Component {
  state = {
    submitLoading: false
  };

  handleSubmit = (e) => {
    e.stopPropagation();
    const { form: { validateFieldsAndScroll } } = this.props;
    validateFieldsAndScroll(async (errors, values) => {
      if (!errors) {
        // 前端写死的一个选项 后端不处理这个 默认传值时去除
        let currentConfig = JSON.parse(JSON.stringify(values));
        try {
          currentConfig.faPiaoPasteFreeConditions.splice(currentConfig.faPiaoPasteFreeConditions.indexOf('WEB_INVOICE_FAPIAO_NOTICKET'), 1);
        }catch (e) {}
        this.setState({ submitLoading: true });
        try {
          await requestAndExpenseService.setFreePrintPasteConfig(currentConfig);
          this.props.close(true);
        }
        catch (e) {
        }
        this.setState({ submitLoading: false });
      }
    });
  };

  handleCancel = () => {
    this.props.close();
  };

  render() {
    const { submitLoading } = this.state;
    const { form: { getFieldDecorator, getFieldValue }, params } = this.props;
    const formItemLayout = { labelCol: { span: 7 }, wrapperCol: { span: 15, offset: 1 } };
    let currentParams = {};
    try {
      currentParams = JSON.parse(JSON.stringify(params));
      currentParams.faPiaoPasteFreeConditions.push('WEB_INVOICE_FAPIAO_NOTICKET');
    }catch (e) {}
    return (
      <section className="print-free">
        <Form className="new-form">
          {
            formList.map(item => {
              if ((['expenseReportPrintFreeConditions', 'faPiaoPasteFreeConditions'].includes(item.dataIndex) && !getFieldValue('expenseReportPrintFreeEnabled'))
                || (item.dataIndex === 'faPiaoPasteFreeConditions' && !getFieldValue('expenseReportPrintFreeConditions').includes('INVOICE_FAPIAO_PASTE_FREE'))) {
                return null;
              }

                return (
                  <Form.Item label={item.name} {...formItemLayout} extra={item.extra}>
                    {getFieldDecorator(item.dataIndex, {
                      rules: [{
                        required: !!item.required,
                        message: messages('common.select')/*请选择*/
                      }],
                      initialValue: !!currentParams.id ? currentParams[item.dataIndex] : item.initValue
                    })(React.createElement(item.component, item.params, item.child))}
                  </Form.Item>
                );
              }
            )
          }
          <Affix offsetBottom={0} className="slide-footer">
            <Button type="primary" loading={submitLoading} htmlType="submit" onClick={this.handleSubmit}>
              {messages('common.save')/*保存*/}
            </Button>
            <Button onClick={this.handleCancel}>
              {messages('common.cancel')/*取消*/}
            </Button>
          </Affix>
        </Form>
      </section>
    );
  }
}

PrintFree.propTypes = {};

export default PrintFree;
